package com.zzty.serviceimpl;

import com.zzty.common.ReadFile;
import com.zzty.common.Split;
import com.zzty.dao.QuestionDao;
import com.zzty.dao.SelfExamDao;
import com.zzty.dao.SelfExamTransactionDao;
import com.zzty.daoimpl.QuestionDaoImpl;
import com.zzty.daoimpl.SelfExamDaoImpl;
import com.zzty.daoimpl.SelfExamTransactionDaoImpl;
import com.zzty.entity.*;
import com.zzty.service.SelfExamTransactionService;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SelfExamTransactionServiceImpl implements SelfExamTransactionService {
//    public Integer startSelfExam(int userId) {
//        System.out.println("竞赛开始");
//        SelfExamDao selfExamDaoImpl=new SelfExamDaoImpl();
//        Boolean checkLogIn =selfExamDaoImpl.checkLogIn(userId);
//        System.out.println("是否登陆："+checkLogIn);
//        Question question=new Question();
//        if(checkLogIn==false){
//            question.setId(selfExamDaoImpl.addSelfTransaction(userId));
//        }else {
//            System.out.println("已开始竞赛");
//        }
//
//        return question.getId();
//    }


//    public void endSelfExam(Integer userId,String answerUrl) {
//
//    }


//    public List historyQuestion() {
//        SelfExamDaoImpl selfExamDao=new SelfExamDaoImpl();
//        List<SelfQuestion> questions=null;
//        if(selfExamDao.searchSelfQuestion()==null){
//            return null;
//        }else {
//            questions=selfExamDao.searchSelfQuestion();
//        }
//        return questions;
//    }

    public List<SelfExamPage> getSelfExamToTable() {
        List<SelfExamPage> sep = new ArrayList<SelfExamPage>();
        SelfExamTransactionDao sed = new SelfExamTransactionDaoImpl();
        QuestionDao questionDao = new QuestionDaoImpl();
        List<SelfExam> selfExams = sed.getSelfExamToTable();
        if (selfExams != null) {
            for (SelfExam selfExam : selfExams) {
                Split split = new Split();
                List<Integer> ss = split.getSplit(selfExam.getQuestionId());
                for (Integer s : ss) {
                    String content = questionDao.getContentById(s);
                    SelfExamPage selfExamPage = new SelfExamPage();
                    selfExamPage.setDate(selfExam.getDate());
                    selfExamPage.setExamId(selfExam.getId());
                    selfExamPage.setQuestionContent(content);
                    sep.add(selfExamPage);
                }
            }
            for(int i=0;i<sep.size();i++){
                for(int j=i+1;j<sep.size();j++){
                    if(sep.get(i).getDate().equals(sep.get(j).getDate())){
                        String content1=sep.get(i).getQuestionContent();
                        String content2=sep.get(j).getQuestionContent();
                        sep.get(i).setQuestionContent(content1+"；"+content2);
                        sep.remove(j);
                        j--;
                    }
                }
            }
        }
        return sep;
    }

//    public List getContentByDate(Date date){
//        return null;
//    }


    public List searchAnswerById(int examId,int userId) {
        SelfExamTransactionDao selfExamDao = new SelfExamTransactionDaoImpl();
        String answerUrl = selfExamDao.searchAnswerUrlById(examId,userId);
        File file = new File(answerUrl);
        String s[] = file.list();
        List<AnswerPage> answerPages = new ArrayList<AnswerPage>();
        for (String temp : s) {
            if(temp!=null){
                AnswerPage answerPage = new AnswerPage();
                ReadFile readFile = new ReadFile();
                StringBuilder answer = readFile.readJava(answerUrl +"/"+ temp);
                answerPage.setAnswer(answer);
                answerPage.setName(temp);
                answerPages.add(answerPage);
            }
        }
        return answerPages;
    }

    public List<ScorePage> createTable(){
        List<ScorePage> sp=new ArrayList<ScorePage>();
        SelfExamTransactionDao sed = new SelfExamTransactionDaoImpl();
        List<SelfExamTransaction> sets = sed.createTable();
        if (sets != null) {
            for (SelfExamTransaction selfExamTransaction : sets) {
                ScorePage scorePage = new ScorePage();
                String date = selfExamTransaction.getStartTime().substring(0,10);
                String name = sed.getNameById(selfExamTransaction.getUserId());
                scorePage.setDate(date);
                scorePage.setName(name);
                scorePage.setStartTime(selfExamTransaction.getStartTime());
                scorePage.setSpendTime(selfExamTransaction.getSpendTime());
                scorePage.setScore(selfExamTransaction.getScore());
                sp.add(scorePage);

            }
        }
        return sp;
    }


}




//    public void checkWork() {
//
//    }
//}
